#coding=utf-8
import xlrd
from xlutils.copy import copy
import time
import os
import re
class ExcelUtil:
    def __init__(self,excel_path=None,index=None):
        if excel_path == None:
            dir01 = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
            self.excel_path = os.path.join(dir01+'\\config\\casedata.xls')
        else:
            self.excel_path = excel_path
        if index == None:
            index = 0
        self.data = xlrd.open_workbook(self.excel_path)
        self.table = self.data.sheets()[index]
        #[[],[]]

    # 获取excel数据，按照每行一个list，添加到一个大的list里面
    def get_data(self):
        result = []
        rows = self.get_lines()
        if rows !=None:
            for i in range(1,rows):
                col = self.table.row_values(i)
                result.append(col)
            return result
        return None


    #获取excel行数
    def get_lines(self):
        #行数
        rows = self.table.nrows
        if rows>=1:
            return rows
        return None

    #获取单元格的数据
    def get_col_value(self,row,col):
        #print 
        if self.get_lines()>row:
            data = self.table.cell(row,col).value
            return data
        return None


    #写入数据
    def write_value(self,row,value):
        read_value = xlrd.open_workbook(self.excel_path)
        write_data = copy(read_value)
        write_data.get_sheet(0).write(row,9,value)
        write_data.save(self.excel_path)
        time.sleep(1)

    def data_text(self):
        file_path = 'F:\\A_RuanJian\\Py_project\\MSS_selenium_2\\config\\123.txt'
        list01 = []
        with open(file_path,'r') as fi:
            for i in fi:
                a01=i.strip()
                list_data = []
                line = a01.strip().replace("\n", "")
                list_data.append(line)
                list01.append(list_data)
        return list01

if __name__ == '__main__':
    ex = ExcelUtil()
    a01 = ex.data_text()
    print(a01)
    print(type(a01))